/**
 * 按逗号分隔后，应该有多少长度，符合true, 不符合和其他异常返回false
 * @param {*} str 字符串
 * @param {*} length 长度
 */
export function splitToLength(str, length) {
  if (str) {
    const tmp = str.split(',')
    if (tmp.length === length) {
      return true
    }
    return false
  } else {
    return false
  }
}

/**
 * 有无，转boolen, 目前只返回原语句, 什么都不符合返回空
 * 二元运算符不够用
 * @param {*} str
 */
export function BoolStrWithBoolen(str) {
  if (
    str === '有' ||
    str === '有效' ||
    str === '是' ||
    str === '闻' ||
    str === '肿大' ||
    str === '按时' ||
    str === '温暖' ||
    str === '肿胀'
  ) {
    return true
  } else if (
    str === '无' ||
    str === '无效' ||
    str === '否' ||
    str === '未闻' ||
    str === '无肿大' ||
    str === '未按时' ||
    str === '不温' ||
    str === '无肿胀'
  ) {
    return false
  }
  return ''
}

/**
 * 有无，转1,0,
 * @param {*} str
 */
export function BoolStrWithNumber(str) {
  if (str === '有' || str === '是' || str === '有效') {
    return '1'
  } else if (str === '无' || str === '否' || str === '无效') {
    return '0'
  }
  return ''
}

/**
 * 按逗号分隔后的数组
 * @param {*} str 字符串
 */
export function splitToArray(str = '') {
  if (str) {
    const tmp = str.split(',')
    if (tmp && tmp.length > 0) {
      return tmp
    } else {
      return []
    }
  } else {
    return []
  }
}

/**
 * 去掉首位非法字符串
 * @param {*} str 字符串
 */
export function replaceFirstAndLast(str) {
  if (!str) return str
  try {
    if (str.startsWith('，')) str = str.slice(1, str.length)
    if (str.startsWith('，，')) str = str.slice(2, str.length)
    if (str.startsWith('。')) str = str.slice(1, str.length)
    if (str.startsWith(',')) str = str.slice(1, str.length)
    if (str.endsWith('，。')) str = str.slice(0, str.length - 2)
    if (str.endsWith('，')) str = str.slice(0, str.length - 1)
    if (!str.endsWith('。')) str += '。'
    return str
  } catch (error) {
    return str
  }
}

export function replaceRepeatSymbol(str = '') {
  const beforeSymbol1 = '，。'
  const beforeSymbol2 = '，，'
  const beforeSymbol3 = '。。'
  const beforeSymbol4 = '。，'
  const beforeSymbol5 = '：,'
  const beforeSymbol6 = '。，'
  const beforeSymbol7 = '。,'
  const beforeSymbol8 = ', ，'

  const afterSymbol1 = '。'
  const afterSymbol2 = '，'
  const afterSymbol3 = '。'
  const afterSymbol4 = '。'
  const afterSymbol5 = '：'
  const afterSymbol6 = '。'
  const afterSymbol7 = '。'
  const afterSymbol8 = '，'
  let tmp = ''
  tmp = str.replaceAll(beforeSymbol1, afterSymbol1)
  tmp = tmp.replaceAll(beforeSymbol2, afterSymbol2)
  tmp = tmp.replaceAll(beforeSymbol3, afterSymbol3)
  tmp = tmp.replaceAll(beforeSymbol4, afterSymbol4)
  tmp = tmp.replaceAll(beforeSymbol5, afterSymbol5)
  tmp = tmp.replaceAll(beforeSymbol6, afterSymbol6)
  tmp = tmp.replaceAll(beforeSymbol7, afterSymbol7)
  tmp = tmp.replaceAll(beforeSymbol8, afterSymbol8)
  return tmp
  // function replaceSymbol(string = '', beforeSymbol, afterSymbol) {
  //   string.indexOf(beforeSymbol)
  // }
}

export function checkMaxDivideWtihNumber(one, two, coefficient = 4) {
  const number1 = one ? parseFloat(one) : 0
  const number2 = two ? parseFloat(two) : 0

  const maxNumber = Math.max(number1, number2)

  return maxNumber ? maxNumber / coefficient : -1
}
